package com.cros.utilities;
import java.util.Random;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

//para el uso de log se utiliza la libreria commons-logging.jar
public class Password {
	private static Log log = LogFactory.getLog(Password.class);
	private static Password instance = null;
	private static final String charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	private static final String numbers = "0123456789";
	private static final String lowercase = "abcdefghijklmnopqrstuvwxyz";
	private static final String uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	private static final String lowercaseAndUppercase="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	
	public Password getInstance(){
		if(instance == null){
			instance = new Password();
		}
		return instance;
	}
	
	private Password(){
		
	}
  
	public static String getRandomPasswordCharset (int length) throws Exception{
		try{
	        Random rand = new Random (System.currentTimeMillis()) ;
	        StringBuffer sb = new StringBuffer();
	        for (int i = 0 ; i < length ; i ++ ) {
	            int pos = rand.nextInt ( charset.length () ) ;
	            sb. append ( charset. charAt ( pos ) ) ;
	        }
	        return sb.toString();
		}catch(Exception ex){
			log.error("",ex);
			throw ex;
		}
    }
	
	public static String getRandomPasswordNumbers (int length) throws Exception{
		try{
	        Random rand = new Random (System.currentTimeMillis()) ;
	        StringBuffer sb = new StringBuffer () ;
	        for (int i = 0 ; i < length ; i ++ ) {
	            int pos = rand.nextInt ( numbers.length () ) ;
	            sb. append ( numbers. charAt ( pos ) ) ;
	        }
	        return sb.toString();
		}catch(Exception ex){
			log.error("",ex);
			throw ex;
		}
    }
	
	public static String getRandomPasswordLowercase (int length) throws Exception{
		try{
	        Random rand = new Random (System.currentTimeMillis()) ;
	        StringBuffer sb = new StringBuffer () ;
	        for (int i = 0 ; i < length ; i ++ ) {
	            int pos = rand.nextInt ( lowercase.length () ) ;
	            sb. append ( lowercase. charAt ( pos ) ) ;
	        }
	        return sb.toString();
		}catch(Exception ex){
			log.error("",ex);
			throw ex;
		}
    }
	
	public static String getRandomPasswordUppercase (int length) throws Exception{
		try{
	        Random rand = new Random (System.currentTimeMillis()) ;
	        StringBuffer sb = new StringBuffer () ;
	        for (int i = 0 ; i < length ; i ++ ) {
	            int pos = rand.nextInt ( uppercase.length () ) ;
	            sb. append ( uppercase. charAt ( pos ) ) ;
	        }
	        return sb.toString();
		}catch(Exception ex){
			log.error("",ex);
			throw ex;
		}
    }
	
	public static String getRandomPasswordLowercaseAndUppercase (int length) throws Exception{
		try{
	        Random rand = new Random (System.currentTimeMillis()) ;
	        StringBuffer sb = new StringBuffer () ;
	        for (int i = 0 ; i < length ; i ++ ) {
	            int pos = rand.nextInt ( lowercaseAndUppercase.length () ) ;
	            sb. append ( lowercaseAndUppercase. charAt ( pos ) ) ;
	        }
	        return sb.toString();
		}catch(Exception ex){
			log.error("",ex);
			throw ex;
		}
    }
	
}
